const path = require('path');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const htmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
    entry: {
        index: './src/js/index.js'
    },
    output: {
        filename: 'js/[name].[hash].js',
        path: path.resolve(__dirname, './dist')
    },
    module: {
        rules: [
            //js
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            //scss
            {
                // test: /\.scss/,
                // use: ['style-loader', 'css-loader', 'sass-loader']
                test: /\.scss/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'sass-loader']
                })
            },
            // img
            {
                test: /\.(img|png|jpg)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 5000,
                        name: 'img/[name].[ext]',
                        publicPath: '/'
                    }
                }
            }
        ]
    },
    plugins: [
        new ExtractTextWebpackPlugin({
            filename: 'css/[name].css'
        }),
        new htmlWebpackPlugin({
            template: './src/index.html',
            title: '首页'
        })
    ],
    devServer: {
        port: 9090,
        hot: true,
        host: 'localhost',
        open: true,
        //自动打开浏览器,
        before(app) {
            app.get('/apist', (req, res, next) => {
                res.json({
                    code: "11111"
                })
            })
        },
        // proxy: { //设置代理
        //     './以什么开头的'：
        //     '代理到那个端口'

        // }
    }

}